package com.taptech.doufu.ui.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.constant.FragmentLoadType;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.adapter.ItemCpRankAdapter;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.bean.cp.ItemCpRankBean;
import com.taptech.doufu.net.retrofit.api.ApiServer;
import com.taptech.doufu.util.ItemClickSupport;

import java.util.List;

import rx.Observer;

/**
 * author zhangyongcun
 * 16/9/29
 * CP  行
 */

public class CPRankListActivity extends DiaobaoBaseActivity {
    private static final String TAG = CPRankListActivity.class.getSimpleName();

    private TextView mRankTitle;
    private RecyclerView mRanklistRecycle;
    private ItemCpRankAdapter cpRankAdapter;
    private List<ItemCpRankBean.DataBean.ItemsBean> cpRankBeanList;

    private String rankUrl;
    private String rankTitle;

    private ApiServer cpService;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cp_rank_list_layout);
        initView();
        initData();
        rxData();
    }

    private void rxData() {
        if (rankUrl == null) {
            return;
        }
        cpService.getCpRankBean()
                .compose(RxJavaHelper.<ItemCpRankBean>observeOnMainThread(this))
                .subscribe(new Observer<ItemCpRankBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ItemCpRankBean itemCpRankBean) {
                        cpRankBeanList = itemCpRankBean.getData().getItems();
                        cpRankAdapter = new ItemCpRankAdapter(cpRankBeanList);
                        mRanklistRecycle.setAdapter(cpRankAdapter);
                        ItemClickSupport.addTo(mRanklistRecycle).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                            @Override
                            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                                ItemCpRankBean.DataBean.ItemsBean itemBean = cpRankBeanList.get(position);
                                Intent intent = new Intent(CPRankListActivity.this, CPRankActivity.class);
                                intent.putExtra(FragmentLoadType.fragmentTypeName, itemBean.getName());
                                intent.putExtra(Constant.URL,itemBean.getUrl());
                                startActivity(intent);
                            }
                        });
                    }
                });
    }

    private void initData() {
        ApiClient apiClient = new ApiClient();
        apiClient.init(this);
        cpService = apiClient.getService();
    }

    private void initView() {
        rankUrl = getIntent().getStringExtra(Constant.URL);
        rankTitle = getIntent().getStringExtra(FragmentLoadType.fragmentTypeName);

        mRankTitle = (TextView) findViewById(R.id.cp_rank_title);
        mRanklistRecycle = (RecyclerView) findViewById(R.id.cp_rank_list_recycle);
        GridLayoutManager mg = new GridLayoutManager(CPRankListActivity.this, 1);
        mRanklistRecycle.setLayoutManager(mg);
        if (rankTitle != null) {
            mRankTitle.setText(rankTitle);
        }

    }

    public void onHelp(View v) {
        Intent intent = new Intent();
        intent.setClass(CPRankListActivity.this, BrowseActivity.class);
        intent.putExtra(Constant.URL, "http://api.doufu.la/index.php/journal/detail?id=116");
        startActivity(intent);
    }

    public void onClick(View v) {
        finish();
    }

}